<?php
/**
 * Author:  tangxiaowen
 * Date:    2018/12/3 0003 15:21
 * Mail:    846506584@qq.com
 * describe 后台统计类
 */

namespace Admin\Controller\Loans;

use Admin\Controller\System\BaseController;

class SumController extends BaseController
{

    /**
     * 统计报表1 页面
     */
    public function index(){
        $this->display();
    }
	public function exportexcel(){
        $page = I('post.page', 1, 'intval');
        $rows = I('post.rows', 100, 'intval');
        $sort = I('post.sort');
        $order = I('post.order');
        $StartTime = I('post.StartTime', '');
        $EndTime = I('post.EndTime', '');
        $date = $this->getDate($page,$rows,$sort,$order,$StartTime,$EndTime);
        $array = '';
        $array = array_slice($date['dataArr'], $date['start'], $date['rows']);
        $result = [];
//		p($array);die;
        if($array){
            //处理时间参数并查询相关所有待数据
            $time = ['between',$array[19]['Times'].','.$array[0]['Times'].' 23:59:59'];
            $liuTime = ['between',date("Y-m-d",strtotime('-6 day',strtotime($array[19]['Times']))).','.date("Y-m-d",strtotime('-6 day',strtotime($array[0]['Times']))).' 23:59:59'];

            //获取注册用户数据
            $zcWhere['RegTime'] = $time;
            $zcField = ['RegTime'];
            $zcUserData = M('mem_info')->where($zcWhere)->field($zcField)->select();

            //获取时间范围内借款数据
            $jkWhere['ApplyTime'] = $time;
            $jkField = ['ApplyTime,ApplyMoney'];
            $jkOrderData = M('loans_applylist')->where($jkWhere)->field($jkField)->select();

            //获取时间范围的放款数据
            $fkWhere['OpenTime'] = $time;
            $fkField = ['ApplyTime,ApplyMoney,is_fujie,xjnums,OpenTime,ID'];
            $fkOrderData = M('loans_applylist')->where($fkWhere)->field($fkField)->select();

            //获取6天前的放款数据
            $liuWhere['OpenTime'] = $liuTime;
            $liuField = ['ShTime,is_fujie'];
            $liuOrderData = M('loans_applylist')->where($liuWhere)->field($liuField)->select();

            //获取时间范围内的逾期单数
            $yqWhere['YyFkTime'] = $time;
            $yqWhere['LoanStatus'] = 2;//订单状态 0申请中 1放款中 2已放款 3已完成 4已取消 5人工已拒绝 6等待银行打款
            $yqField = ['YyFkTime,is_fujie,ApplyMoney,xjnums,ID,LoanStatus'];
            $yqOrderData = M('loans_applylist')->where($yqWhere)->field($yqField)->select();

            //获取时间范围内的还款数据
            $hkWhere['HkTime'] = $time;
            $hkWhere['PayStatus'] = 1;
            $hkField = ['a.ApplyMoney,a.OpenM,h.HkTime,h.ID'];
            $hkOrderData = M('loans_applylist')->alias('a')
                ->join('xb_loans_hklist h on a.LoanNo=h.LoanNo')
                ->field($hkField)
                ->where($hkWhere)
                ->select();

            //获取时间范围内的续期数据
            $xqWhere['XjTime'] = $time;
            $xqWhere['a.xjnums'] = ['NEQ',0];
            $xqWhere['l.Status'] = 1;
            $xqOrderData = M('loans_applylist')->alias('a')
                ->join('left join xb_loans_xjapplylist l on a.LoanNo=l.LoanNo')
                ->where($xqWhere)
                ->field('a.xjnums,a.ApplyMoney,l.XjTime,l.OrderSn')
//                ->fetchSql(true)
                ->select();

            //处理数据
            for($i=0;$i<count($array);$i++){

                //每日日期区间
                $staTime = strtotime($array[$i]['Times']);
                $endTime = $staTime + 86400;

                //6天前的每日日期区间
                $liuStaTime = $staTime - 518400;
                $liuEndTime = $liuStaTime + 86400;

                $regUsers = 0;//注册用户数
                foreach($zcUserData as $k=>$v){
                    if(strtotime($v['RegTime']) > $staTime && strtotime($v['RegTime']) < $endTime){
                        $regUsers ++;
                        unset($zcUserData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $jkMoney = 0;//借款金额
                $jkMoneys = 0;//借款单数
                foreach($jkOrderData as $k=>$v){
                    if(strtotime($v['ApplyTime']) > $staTime && strtotime($v['ApplyTime']) < $endTime){
                        $jkMoney += $v['ApplyMoney'];
                        $jkMoneys ++;
                        unset($jkOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $fkMoney = 0;//放款金额
                $fkMoneys = 0;//放款单数

                $sjMoney = 0;//首借金额
                $sjMoneys = 0;//首借单数

                $fjMoney = 0;//复借金额
                $fjMoneys = 0;//复借单子
                foreach($fkOrderData as $k=>$v){
                    if(strtotime($v['OpenTime']) > $staTime && strtotime($v['OpenTime']) < $endTime){
                        $fkMoney += $v['ApplyMoney'];
                        $fkMoneys ++;

                        //判断是否首借
                        if($v['is_fujie'] != 1){
                            $sjMoney += $v['ApplyMoney'];
                            $sjMoneys ++;
                        }else{
                            $fjMoney += $v['ApplyMoney'];
                            $fjMoneys ++;
                        }
                        unset($fkOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $liuOrder = 0;//6天前的放款单数
                $liuOrder1 = 0;//6复借
                $liuOrder2 = 0;//6首借
                foreach($liuOrderData as $k=>$v){
                    if(strtotime($v['ShTime']) > $liuStaTime && strtotime($v['ShTime']) < $liuEndTime){
                        $liuOrder ++;
                        if($v['is_fujie'] != 1){
                            $liuOrder1 ++;
                        }else{
                            $liuOrder2 ++;
                        }
                        unset($liuOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $sjyqMoney = 0;//首借到期待还金额
                $sjyqMoneys = 0;//首借到期待还单数

                $fjyqMoney = 0;//复借到期待还金额
                $fjyqMoneys = 0;//复借到期待还单数

                foreach ($yqOrderData as $K => $v) {
                    if (strtotime($v['YyFkTime']) > $staTime && strtotime($v['YyFkTime']) < $endTime) {
                        if($v['xjnums'] == 0){
                            if ($v['is_fujie'] != 1 && $v['xjnums'] == 0) {
                                $sjyqMoney += $v['ApplyMoney'];
                                $sjyqMoneys++;
                            } elseif($v['xj_time'] == 0) {
                                $fjyqMoney += $v['ApplyMoney'];
                                $fjyqMoneys++;
                            }
                            unset($yqOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                        }
                    }
                }

                $hkMoney = 0;//还款金额
                $hkMoneys = 0;//还款单数
                foreach($hkOrderData as $k=>$v){
                    if(strtotime($v['HkTime']) > $staTime && strtotime($v['HkTime']) < $endTime){
                        $hkMoney += $v['ApplyMoney'];
                        $hkMoneys ++;
                        unset($hkOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $xqMoney = 0;//续期金额
                $xqMoneys = 0;//续期笔数
                foreach($xqOrderData as $k=>$v){
                    if(strtotime($v['XjTime']) > $staTime && strtotime($v['XjTime']) < $endTime){
                        $xqMoney += $v['ApplyMoney'];
                        $xqMoneys ++;
                        unset($xqOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $result['rows'][$i] = [
                    'time'        => date('Y-m-d',$staTime),       //日期
                    'regUsers'    => $regUsers,                    //注册人数

                    'jkMoney'     => number_format($jkMoney,2),    //借款金额
                    'jkMoneys'    => $jkMoneys,                    //借款单数

                    'fkMoney'     => number_format($fkMoney,2),    //放款金额
                    'fkMoneys'    => $fkMoneys,                    //放款单数

                    'sjMoney'     => number_format($sjMoney,2),    //首借金额
                    'sjMoneys'    => $sjMoneys,                    //首借单数

                    'sjyqMoney'   => number_format($sjyqMoney,2),  //首借到期金额
                    'sjyqMoneys'  => $sjyqMoneys,                  //首借到期单数
                    'sjyqMoneyss' => (($liuOrder1 != 0)?(sprintf("%.2f",($sjyqMoneys/$liuOrder1) * 100)):0).'%',//首借逾期率

                    'fjMoney'     => number_format($fjMoney,2),    //复借金额
                    'fjMoneys'    => $fjMoneys,                    //复借单数

                    'fjyqMoney'   => number_format($fjyqMoney,2),  //复借到期金额
                    'fjyqMoneys'  => $fjyqMoneys,                  //复借到期单子
                    'fjyqMoneyss' => (($liuOrder2 != 0)?(sprintf("%.2f",($fjyqMoneys/$liuOrder2) * 100)):0).'%',//复借逾期率

                    'hkMoney'     => number_format($hkMoney,2),    //还款金额
                    'hkMoneys'    => $hkMoneys,                    //还款单数

                    'xqMoney'     => number_format($xqMoney,2),    //续期金额
                    'xqMoneys'    => $xqMoneys,                    //续期笔数
                    'xqMoneyss'   => (($liuOrder != 0)?(sprintf("%.2f",($xqMoneys/$liuOrder) * 100)):0).'%',//续借率

                    'liuOrder'    => $liuOrder.'/'.$liuOrder1.'/'.$liuOrder2,//6天前下单数/7天前下单数/首借/复借
                ];
            }
            $result['total'] = count($date['dataArr']);
        }
//      $this->ajaxReturn($result);
    

         //导出拼装
        $html = '<table cellpadding="1" cellspacing="1" border="1" width="100%" bgcolor="#000000;">
            <tr bgcolor="#FFFFFF">
                <td bgcolor="#FFFFFF" align="center" _REQUEST>序号</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>日期</td>
                 <td bgcolor="#FFFFFF" align="center" _REQUEST>注册人数</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>借款金额</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>借款单数</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>放款金额</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>放款单数</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>首借金额</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>首借单数</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>复借金额</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>复借单数</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>首借逾期金额</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>首借逾期单数</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>首借逾期率</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>复借逾期金额</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>复借逾期单数</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>复借逾期率</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>还款金额</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>还款单数</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>续借金额</td>
                <td bgcolor="#FFFFFF" align="center" _REQUEST>续借单数</td>
                  <td bgcolor="#FFFFFF" align="center" _REQUEST>续借率</td>
            </tr>';




      foreach($result['rows'] as $key=>$row){
            $html .= '<tr bgcolor="#FFFFFF">
                <td bgcolor="#FFFFFF" align="center" >'.intval($key+1).'</td>
                <td bgcolor="#FFFFFF" align="center">'.$row['time'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['regUsers'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['jkMoney'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['jkMoneys'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['fkMoney'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['fkMoneys'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['sjMoney'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['sjMoneys'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['fjMoney'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['fjMoneys'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['sjyqMoney'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['sjyqMoneys'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['sjyqMoneyss'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['fjyqMoney'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['fjyqMoneys'].'</td>
                
                <td bgcolor="#FFFFFF" align="center" >'.$row['fjyqMoneyss'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['hkMoney'].'</td>
                <td bgcolor="#FFFFFF" align="center" >'.$row['hkMoneys'].'</td>
                
				
                <td bgcolor="#FFFFFF" align="center" >'.$row['xqMoney'].'</td>
                 <td bgcolor="#FFFFFF" align="center" >'.$row['xqMoneys'].'</td>
                 <td bgcolor="#FFFFFF" align="center" >'.$row['xqMoneyss'].'</td>
            </tr>';
        }

        $html .= '</table>';
        $str_filename = date('Y-m-d', time()).'放款记录列表';
        //$str_filename = iconv('UTF-8', 'GB2312//IGNORE',$str_filename);
        $html = iconv('UTF-8', 'GB2312//IGNORE',$html);
        ob_end_clean();//清除缓存区的内容
        header("Content-type: application/vnd.ms-excel; charset=GBK");
        //header('Content-Type:text/html;charset=utf-8');
        header("Content-Disposition: attachment; filename=$str_filename.xls");
        echo $html;
        exit;
    }

    /**
     * 统计报表 数据
     */
    public function DataList()
    {
        $page = I('post.page', 1, 'intval');
        $rows = I('post.rows', 20, 'intval');
        $sort = I('post.sort');
        $order = I('post.order');
        //搜索条件
        $StartTime = I('post.StartTime', '');
        $EndTime = I('post.EndTime', '');

        //获取日期数据，此为上一届php代码，属于不懂拷贝来用用
        $date = $this->getDate($page,$rows,$sort,$order,$StartTime,$EndTime);

        //数据分页
        $array = '';
        $array = array_slice($date['dataArr'], $date['start'], $date['rows']);
        $result = [];
        if($array){
            //处理时间参数并查询相关所有待数据
            $time = ['between',$array[19]['Times'].','.$array[0]['Times'].' 23:59:59'];
            $liuTime = ['between',date("Y-m-d",strtotime('-6 day',strtotime($array[19]['Times']))).','.date("Y-m-d",strtotime('-6 day',strtotime($array[0]['Times']))).' 23:59:59'];

            //获取注册用户数据
            $zcWhere['RegTime'] = $time;
            $zcField = ['RegTime'];
            $zcUserData = M('mem_info')->where($zcWhere)->field($zcField)->select();

            //获取时间范围内借款数据
            $jkWhere['ApplyTime'] = $time;
            $jkField = ['ApplyTime,ApplyMoney'];
            $jkOrderData = M('loans_applylist')->where($jkWhere)->field($jkField)->select();

            //获取时间范围的放款数据
            $fkWhere['OpenTime'] = $time;
            $fkField = ['ApplyTime,ApplyMoney,is_fujie,xjnums,OpenTime,ID'];
            $fkOrderData = M('loans_applylist')->where($fkWhere)->field($fkField)->select();

            //获取6天前的放款数据
            $liuWhere['OpenTime'] = $liuTime;
            $liuField = ['ShTime,is_fujie'];
            $liuOrderData = M('loans_applylist')->where($liuWhere)->field($liuField)->select();

            //获取时间范围内的逾期单数
            $yqWhere['YyFkTime'] = $time;
            $yqWhere['LoanStatus'] = 2;//订单状态 0申请中 1放款中 2已放款 3已完成 4已取消 5人工已拒绝 6等待银行打款
            $yqField = ['YyFkTime,is_fujie,ApplyMoney,xjnums,ID,LoanStatus'];
            $yqOrderData = M('loans_applylist')->where($yqWhere)->field($yqField)->select();

            //获取时间范围内的还款数据
            $hkWhere['HkTime'] = $time;
            $hkWhere['PayStatus'] = 1;
            $hkField = ['a.ApplyMoney,a.OpenM,h.HkTime,h.ID'];
            $hkOrderData = M('loans_applylist')->alias('a')
                ->join('xb_loans_hklist h on a.LoanNo=h.LoanNo')
                ->field($hkField)
                ->where($hkWhere)
                ->select();

            //获取时间范围内的续期数据
            $xqWhere['XjTime'] = $time;
            $xqWhere['a.xjnums'] = ['NEQ',0];
            $xqWhere['l.Status'] = 1;
            $xqOrderData = M('loans_applylist')->alias('a')
                ->join('left join xb_loans_xjapplylist l on a.LoanNo=l.LoanNo')
                ->where($xqWhere)
                ->field('a.xjnums,a.ApplyMoney,l.XjTime,l.OrderSn')
//                ->fetchSql(true)
                ->select();

            //处理数据
            for($i=0;$i<count($array);$i++){

                //每日日期区间
                $staTime = strtotime($array[$i]['Times']);
                $endTime = $staTime + 86400;

                //6天前的每日日期区间
                $liuStaTime = $staTime - 518400;
                $liuEndTime = $liuStaTime + 86400;

                $regUsers = 0;//注册用户数
                foreach($zcUserData as $k=>$v){
                    if(strtotime($v['RegTime']) > $staTime && strtotime($v['RegTime']) < $endTime){
                        $regUsers ++;
                        unset($zcUserData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $jkMoney = 0;//借款金额
                $jkMoneys = 0;//借款单数
                foreach($jkOrderData as $k=>$v){
                    if(strtotime($v['ApplyTime']) > $staTime && strtotime($v['ApplyTime']) < $endTime){
                        $jkMoney += $v['ApplyMoney'];
                        $jkMoneys ++;
                        unset($jkOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $fkMoney = 0;//放款金额
                $fkMoneys = 0;//放款单数

                $sjMoney = 0;//首借金额
                $sjMoneys = 0;//首借单数

                $fjMoney = 0;//复借金额
                $fjMoneys = 0;//复借单子
                foreach($fkOrderData as $k=>$v){
                    if(strtotime($v['OpenTime']) > $staTime && strtotime($v['OpenTime']) < $endTime){
                        $fkMoney += $v['ApplyMoney'];
                        $fkMoneys ++;

                        //判断是否首借
                        if($v['is_fujie'] != 1){
                            $sjMoney += $v['ApplyMoney'];
                            $sjMoneys ++;
                        }else{
                            $fjMoney += $v['ApplyMoney'];
                            $fjMoneys ++;
                        }
                        unset($fkOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $liuOrder = 0;//6天前的放款单数
                $liuOrder1 = 0;//6复借
                $liuOrder2 = 0;//6首借
                foreach($liuOrderData as $k=>$v){
                    if(strtotime($v['ShTime']) > $liuStaTime && strtotime($v['ShTime']) < $liuEndTime){
                        $liuOrder ++;
                        if($v['is_fujie'] != 1){
                            $liuOrder1 ++;
                        }else{
                            $liuOrder2 ++;
                        }
                        unset($liuOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $sjyqMoney = 0;//首借到期待还金额
                $sjyqMoneys = 0;//首借到期待还单数

                $fjyqMoney = 0;//复借到期待还金额
                $fjyqMoneys = 0;//复借到期待还单数

                foreach ($yqOrderData as $K => $v) {
                    if (strtotime($v['YyFkTime']) > $staTime && strtotime($v['YyFkTime']) < $endTime) {
                        if($v['xjnums'] == 0){
                            if ($v['is_fujie'] != 1 && $v['xjnums'] == 0) {
                                $sjyqMoney += $v['ApplyMoney'];
                                $sjyqMoneys++;
                            } elseif($v['xj_time'] == 0) {
                                $fjyqMoney += $v['ApplyMoney'];
                                $fjyqMoneys++;
                            }
                            unset($yqOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                        }
                    }
                }

                $hkMoney = 0;//还款金额
                $hkMoneys = 0;//还款单数
                foreach($hkOrderData as $k=>$v){
                    if(strtotime($v['HkTime']) > $staTime && strtotime($v['HkTime']) < $endTime){
                        $hkMoney += $v['ApplyMoney'];
                        $hkMoneys ++;
                        unset($hkOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $xqMoney = 0;//续期金额
                $xqMoneys = 0;//续期笔数
                foreach($xqOrderData as $k=>$v){
                    if(strtotime($v['XjTime']) > $staTime && strtotime($v['XjTime']) < $endTime){
                        $xqMoney += $v['ApplyMoney'];
                        $xqMoneys ++;
                        unset($xqOrderData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $result['rows'][$i] = [
                    'time'        => date('Y-m-d',$staTime),       //日期
                    'regUsers'    => $regUsers,                    //注册人数

                    'jkMoney'     => number_format($jkMoney,2),    //借款金额
                    'jkMoneys'    => $jkMoneys,                    //借款单数

                    'fkMoney'     => number_format($fkMoney,2),    //放款金额
                    'fkMoneys'    => $fkMoneys,                    //放款单数

                    'sjMoney'     => number_format($sjMoney,2),    //首借金额
                    'sjMoneys'    => $sjMoneys,                    //首借单数

                    'sjyqMoney'   => number_format($sjyqMoney,2),  //首借到期金额
                    'sjyqMoneys'  => $sjyqMoneys,                  //首借到期单数
                    'sjyqMoneyss' => (($liuOrder1 != 0)?(sprintf("%.2f",($sjyqMoneys/$liuOrder1) * 100)):0).'%',//首借逾期率

                    'fjMoney'     => number_format($fjMoney,2),    //复借金额
                    'fjMoneys'    => $fjMoneys,                    //复借单数

                    'fjyqMoney'   => number_format($fjyqMoney,2),  //复借到期金额
                    'fjyqMoneys'  => $fjyqMoneys,                  //复借到期单子
                    'fjyqMoneyss' => (($liuOrder2 != 0)?(sprintf("%.2f",($fjyqMoneys/$liuOrder2) * 100)):0).'%',//复借逾期率

                    'hkMoney'     => number_format($hkMoney,2),    //还款金额
                    'hkMoneys'    => $hkMoneys,                    //还款单数

                    'xqMoney'     => number_format($xqMoney,2),    //续期金额
                    'xqMoneys'    => $xqMoneys,                    //续期笔数
                    'xqMoneyss'   => (($liuOrder != 0)?(sprintf("%.2f",($xqMoneys/$liuOrder) * 100)):0).'%',//续借率

                    'liuOrder'    => $liuOrder.'/'.$liuOrder1.'/'.$liuOrder2,//6天前下单数/7天前下单数/首借/复借
                ];
            }
            $result['total'] = count($date['dataArr']);
        }
        $this->ajaxReturn($result);
    }

    /**
     * 渠道统计 页面
     */
    public function operator(){

        //获取所有渠道数据并分配页面渲染
        $where['IsDel']  = 0;
        $where['Status'] = 1;
        $channel = M('tg_admin')->where($where)->field('ID,UserName')->select();
        $this->assign('channel',$channel);

        $this->display();
    }

    /**
     * 渠道统计 数据
     */
    public function operators(){
        $page = I('post.page', 1, 'intval');
        $rows = I('post.rows', 20, 'intval');
        $sort = I('post.sort');
        $order = I('post.order');

        $tgadminID = I('post.TgadminID');//推广渠道ID
        $ids = [];
        if($tgadminID && $tgadminID != -5){
            $ids = [$tgadminID];
        }else{
            $data = M('tg_admin')->where(['IsDel' => 0,'Status' => 1])->field(['ID'])->select();
            $ids = [];
            foreach($data as $k => $v){
                $ids[$k] = $v['ID'];
            }
        }

        $where['IsDel']  = 0;
        $where['Status'] = 1;
        $channel = M('tg_admin')->where($where)->field('ID,UserName')->select();

        //搜索条件
        $StartTime = I('post.StartTime', '');
        $EndTime = I('post.EndTime', '');

        //获取日期数据，此为上一届php代码，属于不懂拷贝来用用
        $date = $this->getDate($page,$rows,$sort,$order,$StartTime,$EndTime);

        //数据分页
        $array = '';
        $array = array_slice($date['dataArr'], $date['start'], $date['rows']);
        $result = [];
        if($array) {

            //处理时间参数并查询相关所有待数据
            $time = ['between', $array[19]['Times'] . ',' . $array[0]['Times'] . ' 23:59:59'];

            //获取当日渠道注册会员数
            $usWhere['RegTime'] = $time;
            $usField = ['RegTime,TgadminID,ID'];
            $usWhere['TgadminID'] = ['in',$ids];
            $userData = M('mem_info')->where($usWhere)->field($usField)->select();

            //获取当日渠道会员的订单数
            $orWhere['RegTime'] = $time;
            $orWhere['m.TgadminID'] = ['in',$ids];
            $orField = ['m.RegTime,a.ApplyTime,a.ShTime,a.OpenTime,a.OpenM,a.is_fujie,a.OrderSn'];
            $orderData = M('mem_info')->alias('m')
                ->join('xb_loans_applylist a on m.ID=a.UserID')
                ->where($orWhere)
                ->field($orField)
                ->select();

            //获取时间范围内的续借订单数据
            $xjWhere['x.XjTime'] = $time;
            $xjWhere['x.PayStatus'] = 1;//已支付
            $xjWhere['m.TgadminID'] = ['in',$ids];
            $xjField = ['x.XjTime,a.OrderSn'];
            $xjOrderData = M('mem_info')->alias('m')
                ->join('xb_loans_applylist a on m.ID=a.UserID')
                ->join('xb_loans_xjapplylist x on a.OrderSn=x.LoanNo')
                ->where($xjWhere)
                ->field($xjField)
                ->select();

            //获取时间范围内的还款订单数据
            $hkWhere['h.HkTime'] = $time;
            $hkWhere['h.PayStatus'] = 1;//已支付
            $hkWhere['m.TgadminID'] = ['in',$ids];
            $hkField = ['h.HkTime,a.OrderSn'];
            $hkOrderData = M('mem_info')->alias('m')
                ->join('xb_loans_applylist a on m.ID=a.UserID')
                ->join('xb_loans_hklist h on a.OrderSn=h.LoanNo')
                ->where($hkWhere)
                ->field($hkField)
                ->select();

            //处理每日数据
            for($i=0;$i<count($array);$i++){

                //每日日期区间
                $staTime = strtotime($array[$i]['Times']);
                $endTime = $staTime + 86400;

                $zcNum = 0;//注册数
                foreach($userData as $k=>$v){
                    if(strtotime($v['RegTime']) > $staTime && strtotime($v['RegTime']) < $endTime){
                        $zcNum ++;
                        unset($userData[$k]);
                    }
                }

                $jjNum = 0;//进件数
                $shNum = 0;//审核单数
                $fkNum = 0;//放款单数
                $fkMoney = 0;//放款金额
                $sjNum = 0;//首借单数
                $fjNum = 0;//复借单数
                foreach($orderData as $k=>$v){
                    if(strtotime($v['ApplyTime']) > $staTime && strtotime($v['ApplyTime']) < $endTime){
                        $jjNum ++;
                    }
                    if(strtotime($v['ShTime']) > $staTime && strtotime($v['ShTime']) < $endTime){
                        $shNum ++;
                    }
                    if(strtotime($v['OpenTime']) > $staTime && strtotime($v['OpenTime']) < $endTime){
                        $fkNum ++;
                        $fkMoney += $v['OpenM'];
                        if($v['is_fujie'] == 0){
                            $sjNum ++;
                        }else{
                            $fjNum ++;
                        }
                    }
                }

                $xjNum = 0;//续借单数
                foreach($xjOrderData as $k=>$v){
                    if(strtotime($v['XjTime']) > $staTime && strtotime($v['XjTime']) < $endTime){
                        $xjNum ++;
                    }
                }

                $hkNum = 0;//还款单数
                foreach($hkOrderData as $k=>$v){
                    if(strtotime($v['HkTime']) > $staTime && strtotime($v['HkTime']) < $endTime){
                        $hkNum ++;
                    }
                }

                $result['rows'][$i] = [
                    'time'   => date('Y-m-d',$staTime),//日期
                    'zcNum'  => $zcNum,//注册数
                    'jjNum'  => $jjNum,//进件数
                    'shNum'  => $shNum,//审核单数
                    'fkNum'  => $fkNum,//放款单数
                    'fkMoney'  => number_format($fkMoney,2),//放款金额
                    'sjNum'  => $sjNum,//首借单数
                    'fjNum'  => $fjNum,//复借单数
                    'xjNum'  => $xjNum,//续借单数
                    'hkNum'  => $hkNum,//还款单数
                ];
            }
            $result['total'] = count($date['dataArr']);
        }
        $this->ajaxReturn($result);
    }

    /**
     * 利润统计1
     */
    public function profit(){

        echo '利润统计1';

        $this->display();
    }

    /**
     * 风控绩效统计 页面
     */
    public function risk(){

        //分配所有审核专员数据
        $where['RoleID'] = 7;
        $where['IsDel']  = 0;
        $where['Status'] = 1;
        $auditorData = M('sys_administrator')->where($where)->select();

        $this->assign('auditorData',$auditorData);
        $this->display();
    }

    /**
     * 风控绩效统计 数据
     */
    public function risks()
    {
        $page = I('post.page', 1, 'intval');
        $rows = I('post.rows', 20, 'intval');
        $sort = I('post.sort');
        $order = I('post.order');
        $auditorId = I('post.auditorId/d');//审核员ID

        //搜索条件
        $StartTime = I('post.StartTime', '');
        $EndTime = I('post.EndTime', '');

        //获取日期数据，此为上一届php代码，属于不懂拷贝来用用
        $date = $this->getDate($page,$rows,$sort,$order,$StartTime,$EndTime);

        //数据分页
        $array = '';
        $array = array_slice($date['dataArr'], $date['start'], $date['rows']);
        $result = [];
        if($array){

            //处理时间参数并查询相关所有待数据
            $time = ['between',$array[19]['Times'].','.$array[0]['Times'].' 23:59:59'];
            $liuTime = ['between',date("Y-m-d",strtotime('-6 day',strtotime($array[19]['Times']))).','.date("Y-m-d",strtotime('-6 day',strtotime($array[0]['Times']))).' 23:59:59'];

            //获取时间范围内审核数据
            if($auditorId && $auditorId != -5){
                $shWhere['ServiceID'] = $auditorId;
            }
            $shWhere['ShTime'] = $time;
            $shWhere['LoanStatus'] = ['in','1,2,6'];//1审核通过 2已放款 6已放款等待银行打款
            $shOrderData = M('loans_applylist')->where($shWhere)->select();

            //获取时间范围内下单数据
            if($auditorId && $auditorId != -5){
                $xdWhere['ServiceID'] = $auditorId;
            }
            $xdWhere['ApplyTime'] = $time;
            $shWhere['LoanStatus'] = ['NEQ',4];//4用户已取消的订单状态
            $xdOrderData = M('loans_applylist')->where($xdWhere)->select();

            //获取时间范围内还款订单数据
            if($auditorId && $auditorId != -5){
                $hkWhere['ServiceID'] = $auditorId;
            }
            $hkWhere['HkTime'] = $time;
            $hkWhere['PayStatus'] = 1;
            $hkOrderData = M('loans_applylist')->alias('a')
                ->join('xb_loans_hklist h on a.LoanNo=h.LoanNo')
                ->field('h.HkTime,h.ID')
                ->where($hkWhere)
                ->select();

            //获取时间范围内的续期单数
            if($auditorId && $auditorId != -5){
                $xqWhere['ServiceID'] = $auditorId;
            }
            $xqWhere['XjTime'] = $time;
            $xqOrderData = M('loans_applylist')->alias('a')
                ->join('xb_loans_xjapplylist l on a.LoanNo=l.LoanNo')
                ->field('l.XjTime,l.ID')
                ->select();

            //获取时间范围内的逾期单数
            $yqWhere['YyFkTime'] = $time;
            $yqWhere['LoanStatus'] = 2;
            $yqOrderData = M('loans_applylist')->where($yqWhere)->select();

            //获取六天前下单数据
            if($auditorId && $auditorId != -5){
                $liuWhere['ServiceID'] = $auditorId;
            }
            $liuWhere['ShTime'] = $liuTime;
            $liuOrderData = M('loans_applylist')->where($liuWhere)->select();

            //处理数据
            for($i=0;$i<count($array);$i++) {

                //每日日期区间
                $staTime = strtotime($array[$i]['Times']);
                $endTime = $staTime + 86400;
                //6天前的每日日期区间
                $liuStaTime = $staTime - 518400;
                $liuEndTime = $liuStaTime + 86400;

                $audit = 0;//审核单数
                foreach($shOrderData as $k=>$v){
                    if(strtotime($v['ShTime']) > $staTime && strtotime($v['ShTime']) < $endTime){
                        $audit ++ ;
                        unset($shOrderData[$k]);
                    }
                }

                $order = 0;//申请单数
                foreach($xdOrderData as $k=>$v){
                    if(strtotime($v['ApplyTime']) > $staTime && strtotime($v['ApplyTime']) < $endTime){
                        $order ++ ;
                        unset($xdOrderData[$k]);
                    }
                }

                $repayment = 0;//还款单数
                foreach($hkOrderData as $k=>$v){
                    if(strtotime($v['HkTime']) > $staTime && strtotime($v['HkTime']) < $endTime){
                        $repayment ++;
                        unset($hkOrderData[$k]);
                    }
                }

                $renewal   = 0;//续期单数
                foreach($xqOrderData as $K=>$v){
                    if(strtotime($v['XjTime']) > $staTime && strtotime($v['XjTime']) < $endTime){
                        $renewal ++;
                        unset($xqOrderData[$k]);
                    }
                }

                $dayOrder  = 0;//每天对应6天前的审核通过单数
                foreach($liuOrderData as $k=>$v){
                    if(strtotime($v['ShTime']) > $liuStaTime && strtotime($v['ShTime']) < $liuEndTime){
                        $dayOrder ++;
                        unset($liuOrderData[$k]);
                    }
                }

                $expect = 0;//逾期单数
                foreach($yqOrderData as $K=>$v){
                    if(strtotime($v['YyFkTime']) > $staTime && strtotime($v['YyFkTime']) < $endTime){
                        $expect ++;
                        unset($yqOrderData[$k]);
                    }
                }

                $result['rows'][$i] = [
                    'time'       => date('Y-m-d',$staTime),
                    'audit'      => $audit,     //审核单数
                    'order'      => $order,     //下单单数
                    'repayment'  => $repayment, //还款单数
                    'renewal'    => $renewal,   //续期单数
                    'expect'     => $expect,    //逾期单数
                    'expectRate' => (($dayOrder != 0)?(sprintf("%.2f",($expect/$dayOrder) * 100)):0).'%',//逾期率
                    'dayOrder'  => $dayOrder,   //六天前下单数
                ];
            }
            $result['total'] = count($date['dataArr']);
        }
        $this->ajaxReturn($result);
    }

    /**
     * 进出账统计
     */
    public function count()
    {
        $this->display();
    }

    /**
     * 进出账统计 数据
     */
    public function counts()
    {
        $page = I('post.page', 1, 'intval');
        $rows = I('post.rows', 20, 'intval');
        $sort = I('post.sort');
        $order = I('post.order');

        //搜索条件
        $StartTime = I('post.StartTime', '');
        $EndTime = I('post.EndTime', '');

        //获取日期数据，此为上一届php代码，属于不懂拷贝来用用
        $date = $this->getDate($page, $rows, $sort, $order, $StartTime, $EndTime);

        //数据分页
        $array = '';
        $array = array_slice($date['dataArr'], $date['start'], $date['rows']);
        $result = [];
        if($array){

            //处理时间参数并查询相关所有待数据
            $time = ['between',$array[19]['Times'].','.$array[0]['Times'].' 23:59:59'];

            //获取时间范围的放款数据
            $fkWhere['OpenTime'] = $time;
            $fkWhere['LoanStatus'] = 2;
            $fkField = ['OpenM,is_fujie,xjnums,OpenTime'];
            $fkOrderData = M('loans_applylist')->where($fkWhere)->field($fkField)->select();

            //获取支付记录数据
            $zfWhere['UpdateTime'] = $time;
            $zfWhere['PayStatus'] = 1;
            $zfField = ['OrderAmount,UpdateTime'];
            $zfOrderData = M('loans_paylist')->field($zfField)->select();

            //处理数据
            for($i=0;$i<count($array);$i++) {

                //每日日期区间
                $staTime = strtotime($array[$i]['Times']);
                $endTime = $staTime + 86400;

                $expenditure = 0;//出账
                foreach($fkOrderData as $K=>$v){
                    if(strtotime($v['OpenTime']) > $staTime && strtotime($v['OpenTime']) < $endTime){
                        $num4 = ($v['xjnums'] > 0)?($v['xjnums'] + 1):1;
                        $expenditure += $num4 * $v['OpenM'];
                    }
                }

                $profit = 0;//入账
                foreach($zfOrderData as $K=>$v){
                    if(strtotime($v['UpdateTime']) > $staTime && strtotime($v['UpdateTime']) < $endTime){
                        $profit += $v['OrderAmount'];
                    }
                }

                $result['rows'][$i] = [
                    'time'       => date('Y-m-d',$staTime), //日期
                    'expenditure'=> number_format($expenditure,2), //出账
                    'profit'     => number_format($profit,2),      //入账
                ];
            }
            $result['total'] = count($date['dataArr']);
        }
        //统计行
        if($result['total']){

            //统计放款总金额
            $fkWhere2['LoanStatus'] = 2;
            $fkMoney2 = M('loans_applylist')->where($fkWhere2)->sum('OpenM');

            //统计支付记录总金额
            $zfWhere2['PayStatus'] = 1;
            $zfMoney2 = M('loans_paylist')->where($zfWhere2)->sum('OrderAmount');

            $tempData = [
                'time' => '<span style="color:red;">'.'合计'.'</span>',
                'expenditure' => '<span style="color:red;">'.number_format($fkMoney2,2).'</span>',
                'profit'      => '<span style="color:red;">'.number_format($zfMoney2,2).'</span>',
            ];
            array_unshift($result['rows'],$tempData);
        }
        $this->ajaxReturn($result);
    }

    /**
     * 电话客服绩效统计 页面
     */
    public function mobile(){

        //如果登陆角色为电话客服 仅显示自己的数据 否则显示所有电话客服
        if($_SESSION['AdminInfo']['RoleID'] == 21){
            $where['ID'] = $_SESSION['AdminInfo']['AdminID'];
        }else{
            $where['RoleID'] = 21;
        }

        //分配所有电话客服数据
        $where['IsDel']  = 0;
        $where['Status'] = 1;
        $kfData = M('sys_administrator')->where($where)->select();

        $this->assign('kfData',$kfData);

        $this->display();
    }

    /**
     * 电话客服绩效统计 数据
     */
    public function mobiles(){

        $page = I('post.page', 1, 'intval');
        $rows = I('post.rows', 20, 'intval');
        $sort = I('post.sort');
        $order = I('post.order');
        $kfId = I('post.kfId');

        //搜索条件
        $StartTime = I('post.StartTime', '');
        $EndTime = I('post.EndTime', '');

        //获取日期数据，此为上一届php代码，属于不懂拷贝来用用
        $date = $this->getDate($page, $rows, $sort, $order, $StartTime, $EndTime);

        //数据分页
        $array = '';
        $array = array_slice($date['dataArr'], $date['start'], $date['rows']);
        $result = [];
        if($array){

            //处理时间参数并查询相关所有待数据
            $time = ['between',$array[19]['Times'].','.$array[0]['Times'].' 23:59:59'];

            //获取注册用户数据
            $zcWhere['RegTime'] = $time;
            $zcField = ['RegTime'];
            $zcUserData = M('mem_info')->where($zcWhere)->field($zcField)->select();

            //根据会员注册时间范围内的订单信息
            $orWhere['RegTime'] = $time;
            //验证角色权限 如果是电话客服仅能看到自己数据 否则显示所有
            if($_SESSION['AdminInfo']['RoleID'] == 21){
                $orWhere['m.telServer'] = $_SESSION['AdminInfo']['AdminID'];
            }else{
                if($kfId && $kfId != -5){
                    $orWhere['m.telServer'] = $kfId;
                }
            }
            //获取订单数
            $orField = ['m.RegTime,m.telServer,a.OpenTime,a.is_fujie,a.ApplyMoney'];
            $orderData = M('mem_info')->alias('m')
                ->join('left join xb_loans_applylist a on m.ID=a.UserID')
                ->where($orWhere)
                ->field($orField)
                ->group('m.ID')
//                ->fetchSql()
                ->select();

            //处理数据
            for($i=0;$i<count($array);$i++) {

                //每日日期区间
                $staTime = strtotime($array[$i]['Times']);
                $endTime = $staTime + 86400;

                $zcNum = 0;//注册用户数
                foreach($zcUserData as $k=>$v){
                    if(strtotime($v['RegTime']) > $staTime && strtotime($v['RegTime']) < $endTime){
                        $zcNum ++;
                        unset($zcUserData[$k]);//清除统计过的数据，减少下次遍历，达到优化代码
                    }
                }

                $dhNum = 0;//电话量
                $xdNum = 0;//下款单数
                $fkMoney = 0;//放款金额
                foreach ($orderData as $k => $v) {
                    if (strtotime($v['RegTime']) > $staTime && strtotime($v['RegTime']) < $endTime) {

                        if(!empty($v['telServer'])){
                            $dhNum ++;
                        }

                        if(!empty($v['OpenTime']) && $v['is_fujie'] == 0){
                            $xdNum ++;
                            $fkMoney += $v['ApplyMoney'];
                        }

                        unset($orderData[$k]);
                    }
                }

                $result['rows'][$i] = [
                    'time'   => date('Y-m-d',$staTime),//日期
                    'zcNum'  => $zcNum, //注册数
                    'dhNum'  => $dhNum, //电话量
                    'xdNum'  => $xdNum, //下款单数
                    'fkMoney'=> number_format($fkMoney,2), //下款金额
                ];
            }
            $result['total'] = count($date['dataArr']);
        }
        $this->ajaxReturn($result);
    }

    /**
     * 提醒电话客服绩效统计 页面
     */
    public function remind(){

        //如果登陆角色为提醒客服 仅显示自己的数据 否则显示所有提醒客服
        if($_SESSION['AdminInfo']['RoleID'] == 20){
            $where['ID'] = $_SESSION['AdminInfo']['AdminID'];
        }else{
            $where['RoleID'] = 20;
        }
        $where['RoleID'] = 20;
        $where['IsDel']  = 0;
        $where['Status'] = 1;
        $txKfData = M('sys_administrator')->where($where)->select();

        $this->assign('txKfData',$txKfData);

        $this->display();
    }

    /**
     * 提醒电话客服绩效统计 数据
     */
    public function reminds(){

        $page = I('post.page', 1, 'intval');
        $rows = I('post.rows', 20, 'intval');
        $sort = I('post.sort');
        $order = I('post.order');
        $kfId = I('post.kfId');

        //搜索条件
        $StartTime = I('post.StartTime', '');
        $EndTime = I('post.EndTime', '');

        //获取日期数据，此为上一届php代码，属于不懂拷贝来用用
        $date = $this->getDate($page, $rows, $sort, $order, $StartTime, $EndTime);

        //数据分页
        $array = '';
        $array = array_slice($date['dataArr'], $date['start'], $date['rows']);
        $result = [];
        if($array){

            //分配时间
            $time = ['between',$array[19]['Times'].','.$array[0]['Times'].' 23:59:59'];
            $where['fpTime'] = $time;

            //验证角色权限 如果是提醒客服仅能看到自己数据 否则显示所有
            if($_SESSION['AdminInfo']['RoleID'] == 20){
                $where['a.tingxingID'] = $_SESSION['AdminInfo']['AdminID'];
            }else{
                if($kfId && $kfId != -5){
                    $where['a.tingxingID'] = $kfId;
                }
            }

            //放款时间
            $fkStartTime = I('post.FkStartTime');
            $fkEndTime = I('post.FkEndTime');
            if($fkStartTime && $fkEndTime){
                $where['OpenTime'] = ['between',$fkStartTime.','.$fkEndTime.' 23:59:59'];
            }

            //还款时间
            $hkStartTime = I('post.HkStartTime');
            $hkEndTime = I('post.HkEndTime');
            if($hkStartTime && $hkEndTime){
                $where['HkTime'] = ['between',$hkStartTime.','.$hkEndTime.' 23:59:59'];
            }

            //根据时间范围获取数据
            $fields = ['a.fpTime,a.YyFkTime,a.tingxingID,a.LoanStatus,a.CsadminID,h.HkTime'];
            $data = M('loans_applylist')->alias('a')
                ->join('left join xb_loans_hklist h on a.OrderSn=h.LoanNo')
                ->where($where)
                ->field($fields)
                ->select();

            //处理数据
            $fpNumSum = 0;//合计分配订单数
            $yhNumSum = 0;//合计已还款订单数
            $yqNumSum = 0;//合计逾期还款订单数
            for($i=0;$i<count($array);$i++) {

                //每日日期区间
                $staTime = strtotime($array[$i]['Times']);
                $endTime = $staTime + 86400;

                $fpNum = 0;//分配订单数
                $yhNum = 0;//已还款订单数
                $yqNum = 0;//逾期还款订单数
                foreach($data as $K=>$v){
                    if(strtotime($v['fpTime']) > $staTime && strtotime($v['fpTime']) < $endTime){
                        $fpNum ++;
                        if($v['LoanStatus'] == 3){//已还款
                            $yhNum ++;
                            if(!empty($v['YyFkTime']) && !empty($v['HkTime']) && empty($v['CsadminID'])){//预约还款时间 最终还款时间 催收客服ID
                                if(strtotime($v['YyFkTime']) < strtotime($v['HkTime'])){//过期还款
                                    $yqNum ++;
                                }
                            }
                        }
                    }
                }
                $result['rows'][$i] = [
                    'time'   => date('Y-m-d',$staTime),//日期
                    'fpNum'  => $fpNum, //分配订单数
                    'yhNum'  => $yhNum, //已还款订单数
                    'yqNum'  => $yqNum, //逾期还款订单数
                ];

                $fpNumSum += $fpNum;//合计分配订单数
                $yhNumSum += $yhNum;//合计已还款订单数
                $yqNumSum += $yqNum;//合计逾期还款订单数
            }
            $result['total'] = count($date['dataArr']);
        }

        //合计行
        $tempData = [
            'time'  => '<span style="color:red;">'.'合计'.'</span>',
            'fpNum' => '<span style="color:red;">'.$fpNumSum.'</span>',
            'yhNum' => '<span style="color:red;">'.$yhNumSum.'</span>',
            'yqNum' => '<span style="color:red;">'.$yqNumSum.'</span>',
        ];
        array_unshift($result['rows'],$tempData);

        $this->ajaxReturn($result);
    }

    /**
     * 获取日期参数
     */
    private function getDate($page,$rows,$sort,$order,$StartTime,$EndTime){
        if ($sort && $order) {
            $sort = $sort . ' ' . $order;
        } else {
            $sort = 'ID desc';
        }
        $start = ($page - 1) * $rows;//开始截取位置

        $startday = '';
        $endday = '';
        //算出开始和结束时间
        if (!$StartTime && !$EndTime) {
            $endday = date('Y-m-d');
        }
        if ($StartTime && ($StartTime > date('Y-m-d'))) {
            $endday = date('Y-m-d');
        } elseif ($StartTime) {
            $startday = $StartTime;
            if (!$EndTime) {
                $endday = date('Y-m-d');
            }
        }

        if ($EndTime) {
            $endday = $EndTime;
        }

        $dataArr = array();//总数据记录,单位每天
        if ($startday && $endday) {
            if ($startday != $endday) {
                $dataArr[]['Times'] = $startday;
                for ($i = 1; true; $i++) {
                    $current = '';
                    $current = date('Y-m-d', strtotime("+" . $i . "day", strtotime($startday)));
                    $dataArr[]['Times'] = $current;
                    if ($current >= $endday) {
                        break;
                    }
                }
            } else {
                $dataArr[]['Times'] = $startday;
            }
            //排序 按时间降序
            $dataArr = $this->arraySequence($dataArr, 'Times', $sort = 'SORT_DESC');
        } elseif ($endday) {
            $dataArr[]['Times'] = $endday;
            //向后取19天
            for ($i = 1; true; $i++) {
                $current = '';
                $current = date('Y-m-d', strtotime("-" . $i . "day", strtotime($endday)));
                $dataArr[]['Times'] = $current;
                if ($i >= 19) {
                    break;
                }
            }
        }

        $tempData = [
            'dataArr' => $dataArr,
            'start'   => $start,
            'rows'    => $rows,
        ];
        return $tempData;
    }

    /**
     * 二维数组根据字段进行排序
     * @params array $array 需要排序的数组
     * @params string $field 排序的字段
     * @params string $sort 排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
     */
    private function arraySequence($array, $field, $sort = 'SORT_DESC')
    {
        $arrSort = array();
        foreach ($array as $uniqid => $row) {
            foreach ($row as $key => $value) {
                $arrSort[$key][$uniqid] = $value;
            }
        }
        array_multisort($arrSort[$field], constant($sort), $array);
        return $array;
    }

}
